package org.cypress.rtpack.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.cypress.rtpack.common.core.Result;
import org.cypress.rtpack.common.exception.BizException;
import org.cypress.rtpack.common.exception.ExceptionCode;
import org.cypress.rtpack.entity.PointHistory;
import org.cypress.rtpack.mapper.PointHistoryMapper;
import org.cypress.rtpack.service.IPointHistoryService;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 点位历史记录Service实现类
 * @author getty
 */
@Service
public class PointHistoryServiceImpl extends ServiceImpl<PointHistoryMapper, PointHistory> implements IPointHistoryService {

    @Override
    public Result pageByDescription(String description, long current, long size) {
        Page<PointHistory> page = new Page<>(current, size);
        if (Objects.isNull(description) || description.trim().isEmpty()) {
            // 如果description为空，查询所有记录
            return Result.ok(page(page));
        }
        return Result.ok(baseMapper.pageByDescription(page, description));
    }

    @Override
    public Result getByNodeId(String nodeId) {
        if (Objects.isNull(nodeId)) {
            throw new BizException(ExceptionCode.requestMethodError);
        }
        QueryWrapper<PointHistory> wrapper = new QueryWrapper<>();
        wrapper.eq("nodeId", nodeId);
        PointHistory pointHistory = getOne(wrapper);
        return Result.ok(pointHistory);
    }
}
